<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">	
<html><head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<title>首页</title>
<link href="style/base.css" rel="stylesheet">
<script src="/scripts/jquery/jquery-1.7.2.min.js"></script>
<style>


</style>
<script>

var debug = true;
var PriceDigits = {
	digits : {
		"EURUSD" : 	5
		,"GBPUSD" : 5
		,"USDJPY" : 3
		,"USDCAD" : 5
	}
	,fix : function(symbol,price){
		var rst = "";
		if(!symbol || !price)return rst;
		var _di = this.digits[symbol];
		var _price = price.split(".");
		rst = _price[0]+"."+_price[1].substring(0,_di);
		return rst;
	}
};
var orderSend = function(obj,type){
	var symbol = $(obj).parent().parent().attr("id");
	var url = "/trade/index!orderSend.do";
	var params = "pm.type="+type+"&pm.symbol="+symbol;
	$.getJSON(url,params,function(data){
		if(data=="error"){
			alert("操作时间太短");
		}
	});	
};

var closeOrder = function(obj){
	var oTr = $(obj).parent().parent();
	var magic = oTr.find(".magicNo").val();
	var url = "/trade/index!orderSend.do";
	var params = "pm.type=9&pm.magic="+magic;
	$.getJSON(url,params,function(data){
		if(data=="error"){
			alert("操作时间太短");
		}
	});
};
$(function(){
	
	var initSymbolList = function(){
		var optBtns = "&lt;input type='button' class='btn' onclick='orderSend(this,\"0\")' value='买' /&gt;&amp;nbsp;"
			+"&lt;input type='button' class='btn' onclick='orderSend(this,\"1\")' value='卖' /&gt;";
		$(".symbolTbl .opt").html(optBtns);
	};
	
	var setSymbolInfo = function(data){
		var list = data.split(";"); 
		for(var i=0;i&lt;list.length;i++){
			var symbol = list[i].split(",");
			var oTr = $("#"+symbol[0]);
			oTr.find(".name").html(symbol[0]);
			oTr.find(".sellPrice").html(PriceDigits.fix(symbol[0],symbol[1]));
			oTr.find(".buyPrice").html(PriceDigits.fix(symbol[0],symbol[2])); 
		} 
	};
	
	var setOrderList = function(data){
		var list = data.split(";");
		var orderTbl = $(".orderList");
		//orderTbl.find("tr:gt(0)").remove();
		var newMagicArr = [];
		var oldMagicArr = [];
		for(var i=0;i&lt;list.length;i++){
			var order = list[i].split(",");
			var magic = order[0];
			if(!magic)continue;
			newMagicArr.push(magic);
			var type = order[1];
			type = (type=="0")?"买":"卖";
			var volume = order[2];// 手数
			var symbol = order[3];
			var price = PriceDigits.fix(symbol,order[4]);
			var bidPrice = PriceDigits.fix(symbol,order[5]);
			var askPrice = PriceDigits.fix(symbol,order[6]);
			var nowPrice = (type == "买")?bidPrice:askPrice;
			var profit = parseInt(nowPrice.replace(".","")) - parseInt(price.replace(".",""));
			var exist = false;
			orderTbl.find(".magicNo").each(function(){
				oldMagicArr.push(this.value);
				if(this.value==magic){
					exist = true;
					var _oTr = $(this).parent().parent();
					_oTr.find(".nowPrice").html(nowPrice);
					_oTr.find(".profit").html(profit);
				}
			});
			if(!exist){
				var td = "&lt;tr&gt;&lt;td&gt;"+magic+"&lt;/td&gt;"+"&lt;td&gt;"+type+"&lt;/td&gt;&lt;td&gt;"+volume+"&lt;/td&gt;"
				+"&lt;td&gt;"+symbol+"&lt;/td&gt;&lt;td&gt;"+price+"&lt;/td&gt;&lt;td class='nowPrice'&gt;"+nowPrice+"&lt;/td&gt;&lt;td class='profit'&gt;"+profit+"&lt;/td&gt;";
				td += "&lt;td&gt;&lt;input type='button' class='btn' onclick='closeOrder(this)' value='平仓' /&gt;&lt;input type='hidden' class='magicNo' value='"+magic+"' /&gt;&lt;/td&gt;&lt;/tr&gt;";
				orderTbl.append(td);
				oldMagicArr.push(magic);
			}
		}
		if(oldMagicArr.length == 0 || newMagicArr.length == 0){
			orderTbl.find("tr:gt(0)").remove();
		}
		if(newMagicArr.length!=oldMagicArr.length){
			for(var i=0;i&lt;oldMagicArr.length;i++){
				var _oM = oldMagicArr[i];
				var _flag = false;
				for(var j=0;j&lt;newMagicArr.length;j++){
					if(_oM == newMagicArr[j]){
						_flag = true;
					}
				}
				if(!_flag){
					orderTbl.find(".magicNo").each(function(){
						if(this.value == _oM){
							$(this).parent().parent().remove();
						}
					});
					break;
				}
			}	
		}
	};
	
	var start = function(){
		var url = "/trade/index!start.do";
		$.getJSON(url,"",function(data){
			setSymbolInfo(data.symbolInfo);
			setOrderList(data.orderList);
			if(data.isCloseOrder){
				initAccountInfo();
			}
		});	
		if(!debug)
			window.setTimeout(function(){start();}, 1000);
	};
	
	// 获取帐户信息
	window.initAccountInfo = function(){
		var url = "/trade/index!getAccountInfo.do";
		$.getJSON(url,"",function(data){
			 var info = data.split(",");
			 $("#username").html(info[0]); 
			 $("#money").html(info[1]);
		});	
	};
	
	// 获取帐户信息
	initAccountInfo(); 
	initSymbolList();
	start();
});
</script>
<style>
	.main {width:1003px;margin:10px 0 0 20px;overflow:hidden; zoom:1}
	.symbolDiv {float:left; }
	.symbolDiv table {border:1px solid #cccccc;}
	.symbolTbl{ font-size:12px; }
	.symbolTbl th{background-color:#D4D0C8;border-bottom:0px; } 
	.symbolTbl tr{} 
	.symbolTbl td{border-bottom:1px solid #cccccc;text-align:center;}
	
	.orderListDiv {float:right;}
	.orderListDiv table {border:1px solid #cccccc;}
	.orderList{ font-size:12px; }
	.orderList th{background-color:#D4D0C8;border-bottom:0px; } 
	.orderList tr{} 
	.orderList td{border-bottom:1px solid #cccccc;text-align:center;}
	
	.textLeft {text-algin:left;} 
	.textRight {text-algin:right;}
	.accountInfo {width:1003px;font-size:14px;margin:10px 0 10px 0; font-weight: bold;}
	.btn {width:35px;}
</style> 
</head>
<body> 
<div id="main">
	<!-- top -->
	<div id="top">
		<div class="accountInfo">
			<table>   
				<tbody><tr>
					<td>用户名:</td>
					<td id="username" width="60px;">wsf</td>
					<td>资产总值:</td>
					<td id="money">986</td>
				</tr>
				</tbody>
			</table>
		</div>
	</div>
	<!--// top -->
	
	<!-- nav -->
	<div class="clears"></div>
	<div class="nav"></div>
	<!--// nav -->
	
	<!-- content -->
	<div class="tab_one content">
    	<!-- left -->
		<div class="left">
            <div class="title_pc">
              <h1>商品价格信息</h1>
              <p>&nbsp;</p>
            </div>
            <div class="padder1 desktop">
                <div class="symbolDiv">
                	<table class="symbolTbl">
						<tbody>
						<tr>
							<th width="80px" class="textLeft">商品</th>
							<th width="100px" class="textRight">卖价</th>
							<th width="100px" class="textRight">买价</th>
							<th>操作</th>
						</tr>
						<tr id="EURUSD">
							<td class="name textLeft">EURUSD</td>
							<td class="sellPrice textRight">1.25280</td>
							<td class="buyPrice textRight">1.25290</td>
							<td class="opt"><input type="button" value="买" onclick="orderSend(this,&quot;0&quot;)" class="btn">&nbsp;<input type="button" value="卖" onclick="orderSend(this,&quot;1&quot;)" class="btn"></td>
						</tr>
						<tr id="USDJPY">
							<td class="name textLeft">USDJPY</td>
							<td class="sellPrice textRight">80.550</td>
							<td class="buyPrice textRight">80.580</td>
							<td class="opt"><input type="button" value="买" onclick="orderSend(this,&quot;0&quot;)" class="btn">&nbsp;<input type="button" value="卖" onclick="orderSend(this,&quot;1&quot;)" class="btn"></td>
						</tr>
						<tr id="GBPUSD">
							<td class="name textLeft">GBPUSD</td>
							<td class="sellPrice textRight">1.55650</td>
							<td class="buyPrice textRight">1.55680</td>
							<td class="opt"><input type="button" value="买" onclick="orderSend(this,&quot;0&quot;)" class="btn">&nbsp;<input type="button" value="卖" onclick="orderSend(this,&quot;1&quot;)" class="btn"></td>
						</tr>
						<tr id="USDCAD">
							<td class="name textLeft">USDCAD</td>
							<td class="sellPrice textRight">1.02650</td>
							<td class="buyPrice textRight">1.02670</td>
							<td class="opt"><input type="button" value="买" onclick="orderSend(this,&quot;0&quot;)" class="btn">&nbsp;<input type="button" value="卖" onclick="orderSend(this,&quot;1&quot;)" class="btn"></td>
						</tr>
						</tbody>
					</table>
 				</div>
            </div>
		</div>
	    <!--// left -->	
	    
	     <!-- right -->
		<div class="right">
            <div class="title_pc1">
            	<h1>
                	<A class="blue f14" 
            onclick="pgvSendClick({hottag:'ISD.SHOW.TUIJIAN.MORE'});changesTab('two');" 
            href="javascript:void(0);">订单列表</A>
	            </h1>
            	<span class="more">
                	<A class="blue" 
            onclick="pgvSendClick({hottag:'ISD.SHOW.TUIJIAN.MORE'});changesTab('two');" 
            href="javascript:void(0);">&gt;</A>
	            </span>
            </div>
      		<div class=padder3>
        		<div class="orderListDiv">
	    			
	    		</div>
			</div>
			<div class=clears></div>
		</div>
        <!--// right -->
    </div>
    <!--// content -->
    
    <!-- foot -->
	<div class=foot>Copyright &copy; 亚舟投资 版权所有&nbsp;</div>
    <!--// content -->
</div>
<!--// main -->

<div class="symbolDiv">

</div>

<div class="orderListDiv">
	<table class="orderList">
		<tbody><tr>
			<th width="100px">订单</th>
			<th width="50px">类型</th>
			<th width="50px">手数</th>
			<th width="80px">商品</th>
			<th width="100px">价位</th>
			<th width="100px">现价</th>
			<th width="80px">获利</th>
			<th width="50px">操作</th>
		</tr>
		
	<tr><td>7801630</td><td>买</td><td>0.1</td><td>EURUSD</td><td>1.25590</td><td class="nowPrice">1.25490</td><td class="profit">-100</td><td><input type="button" value="平仓" onclick="closeOrder(this)" class="btn"><input type="hidden" value="7801630" class="magicNo"></td></tr><tr><td>7801631</td><td>买</td><td>0.1</td><td>EURUSD</td><td>1.25590</td><td class="nowPrice">1.25490</td><td class="profit">-100</td><td><input type="button" value="平仓" onclick="closeOrder(this)" class="btn"><input type="hidden" value="7801631" class="magicNo"></td></tr><tr><td>7801632</td><td>买</td><td>0.1</td><td>EURUSD</td><td>1.25590</td><td class="nowPrice">1.25490</td><td class="profit">-100</td><td><input type="button" value="平仓" onclick="closeOrder(this)" class="btn"><input type="hidden" value="7801632" class="magicNo"></td></tr><tr><td>7801633</td><td>买</td><td>0.1</td><td>EURUSD</td><td>1.25590</td><td class="nowPrice">1.25490</td><td class="profit">-100</td><td><input type="button" value="平仓" onclick="closeOrder(this)" class="btn"><input type="hidden" value="7801633" class="magicNo"></td></tr></tbody></table>
</div>
</div>

</body></html>